import { Controller } from 'egg';
import * as moment from 'moment';

const VALIDATION_ADMIN_ADD_USER = {
    username: 'string',
    role: {
        type: 'int',
        allowEmpty: false,
        required: true,
        min: 0,
        max: 4,
    },
    phone: 'string',
    password: {
        type: 'string',
        allowEmpty: true,
        required: true,
    },
};

export default class AdminUserController extends Controller {

    public async getUserById() {
        const { ctx, service } = this;
        const userId = parseInt(ctx.params.userId, 10);
        if (!userId) {
            ctx.helper.invalidateParamsError();
        }
        const user = await service.db.user.findOneById(userId);
        ctx.helper.success(ctx, user);
    }

    public async getUsers() {
        const { ctx, service } = this;
        const page = ctx.params.page ? parseInt(ctx.params.page, 10) : 1;
        const role = parseInt(ctx.params.role, 10);
        if (!role) {
            ctx.helper.invalidateParamsError();
        }
        const users = await service.admin.user.getUsers(role, page, 50);
        const count = await service.admin.user.getUsersCount(role);
        ctx.helper.success(ctx, {
            page,
            pageSize: 50,
            data: users.map((user) => ({
                id: user.id,
                username: user.username,
                avatar: user.avatar,
                created_at: moment(user.created_at).format('YYYY年MM月DD日 HH:mm:SS'),
                updated_at: moment(user.updated_at).format('YYYY年MM月DD日 HH:mm:SS'),
                role: user.role,
                status: user.status,
                openid: user.openid,
                phone: user.phone,
                admin_id: user.admin_id,
                admin_name: user.admin_name,
                teacher_id: user.teacher_id,
                teacher_name: user.teacher_name,
            })),
            count,
        });
    }

    public async addUser() {
        const { ctx, service } = this;
        ctx.validate(VALIDATION_ADMIN_ADD_USER);
        const {
            username,
            phone,
            role,
            password,
        } = ctx.request.body;
        const user = await service.admin.user.addUser(username, phone, role, password, ctx.user.id);
        ctx.helper.success(ctx, user);
    }

    public async updateUser() {
        const { ctx, service } = this;

        ctx.validate({
            id: 'int',
        });
        const {
            id,
            username,
            password,
            avatar,
            role,
            status,
            phone,
            admin_id,
        } = ctx.request.body;
        await service.admin.user.updateUser(id, username,
            password, phone, role, avatar, status, admin_id);
        const user = await service.db.user.findOneById(id);
        ctx.helper.success(ctx, user);
    }

    public async deleteUser() {
        const { ctx, service } = this;
        ctx.validate({
            id: 'int',
        });
        const userId = ctx.request.body.id;
        await service.db.user.deleteUserById(userId);
        ctx.helper.success(ctx);
    }

    public async search() {
        const { ctx, service } = this;
        ctx.validate({
            keyword: 'string',
        });
        const role = parseInt(ctx.params.role, 10);
        if (!role) {
            const keyword: string = ctx.request.body.keyword;
            const result = await service.db.user.searchUsersAndTeachers(keyword);
            ctx.helper.success(ctx, result);
            return;
        }
        const keyword: string = ctx.request.body.keyword;
        const result = await service.db.user.search(role, keyword);
        ctx.helper.success(ctx, result);
    }
}
